<?php

defined('SYSPATH') or die('Access error!');

class Controller_Admin_News extends Controller_Admin_Main
{

    private $news;

    public function before()
    {
        parent::before();
        Assets::js('tinymce', $this->template->media_path . 'js/kindeditor/kindeditor-min.js');
        Assets::css('color', $this->template->media_path . 'css/jquery.bigcolorpicker.css');
        Assets::js('color', $this->template->media_path . 'js/jquery.bigcolorpicker.min.js');
        $this->news = Model_Category::factory('news');
    }

    public function action_add()
    {
        if ($_POST)
        {
            $_POST['add_time'] = Date::get_time();
            $this->save($_POST);
        }
        //新闻分类
        $this->template->assign('category_option', $this->get_category_cache_option('news'));
        //地区分类
        $this->template->assign('area_option', $this->get_category_cache_option('area'));

        //新闻相关推荐
        $recommend_category = $this->get_category_cache_checkbox('recommend', RECOMMEND_CATEGORY, '', 'recommend[]');
        $this->template->assign('checkboxs', $recommend_category);
        $this->template->view('news_add');
    }

    //有待于重构
    public function action_edit()
    {
        $id = $this->request->param('id');
        if ($_POST)
        {
            $this->save($_POST, array('id' => $id));
        }
        //获取新闻的推荐位
        $recommend_list = Model::factory('recommend')->get_recommends_id($id, 'news');
        //获取推荐位类别
        $recommend_category = $this->get_category_cache_checkbox('recommend', RECOMMEND_CATEGORY, $recommend_list, 'recommend[]');
        $this->template->assign('checkboxs', $recommend_category);

        //获取某一篇新闻的详细内容
        $news_detail = $this->news->get_row(array('id' => $id));
        //新闻分类
        $this->template->assign('category_option', $this->get_category_cache_option('news', $news_detail['category_id']));
        //地区分类
        $area_id = empty($news_detail['city_id']) ? $news_detail['province_id'] : $news_detail['city_id'];
        $this->template->assign('area_option', $this->get_category_cache_option('area', $area_id));
        $this->template->assign('data', $news_detail);
        $this->template->view('news_edit');
    }

    public function action_list()
    {
        $this->display_list($this->news->get_news_list());
    }

    public function action_search()
    {
        $category_id = Arr::get($_POST, 'category_id', 0);
        $keywords = Arr::get($_POST, 'keywords', '');
        $data = array();
        if ($category_id != 0)
        {
            $data['category_id'] = $category_id;
        }

        if ($keywords != '')
        {
            $data['name'] = $keywords;
        }
        $news_list = $this->news->get_news_list($data);
        $this->display_list($news_list);
    }

    public function display_list($news_list)
    {
        //缓存的新闻分类--用于搜索
        $this->template->assign('category_option', $this->get_category_cache_option('news'));
        $this->template->assign('news_list', $news_list['list']);
        $this->template->assign('page', $news_list['page']);
        $this->template->view('news_list');
    }

    public function action_delete()
    {
        $id = $this->request->param('id');
        if ($this->news->delete(array('id' => $id)))
        {
            Message::success(__('delete success'));
            $this->request->redirect(Route::get('admin/news')->uri(array('action' => 'list')));
        }
    }

    public function handle_post($data)
    {
        $data['meta_title'] = Arr::get($data, 'meta_title', $data['name']);
        $data['meta_keywords'] = Arr::get($data, 'meta_keywords', $data['name']);
        $data['meta_description'] = Arr::get($data, 'meta_description', Text::limit_chars($data['content'], 150));
        $data['position'] = Arr::get($data, 'position', 9999);
        $data['clicks'] = !empty($data['clicks']) ? $data['clicks'] : Text::random('nozero', 2);
        $data['update_time'] = Date::get_time();
        $data['url'] = Arr::get($data, 'url', '');
        if ($data['url'] != '')
        {
            $data['is_url'] = 1;
        }
        $data['category_id'] = Arr::get($data, 'category_id', NEWS_CATEGORY_ID);
        if (isset($data['area_category_id']))
        {
            $parent_id = Model::factory('category')->get_var(array('id' => $data['area_category_id']), 'parent_id');
            if ($parent_id > NEWS_CATEGORY_ID)
            {
                $data['city_id'] = $data['area_category_id'];
                $data['province_id'] = $parent_id;
            }
            else
            {
                $data['province_id'] = $data['area_category_id'];
            }
            unset($data['area_category_id']);
        }

        unset($data['news_images']);
        unset($data['recommend']);
        unset($data['dosubmit']);
        return parent::handle_post($data);
    }

    public function save($post, $where_arr = array())
    {
        $data = $this->handle_post($post);
        if (!$this->news->validate($data))
        {
            Message::error(__('validate error'));
            $this->request->redirect('admin/news/add');
        }
        if (Arr::validate_arr($where_arr))
        {

            $id = $where_arr['id'];
            if ($this->news->update($data, array('id' => $where_arr['id'])))
            {
                //处理新闻推荐id    
                $recommend_arr = isset($post['recommend']) ? $post['recommend'] : array();
                if (is_array($recommend_arr))
                {
                    Model::factory('recommend')->add_recommend($where_arr['id'], $recommend_arr, 'news');
                }
                Message::success(__('update success'));
                $this->request->redirect('admin/news/edit/' . $where_arr['id']);
            }
        }
        else
        {
            $id = $this->news->insert($data);
            if ($id)
            {
                //处理新闻推荐id    
                $recommend_arr = isset($post['recommend']) ? $post['recommend'] : array();
                if (is_array($recommend_arr))
                {
                    Model::factory('recommend')->add_recommend($id, $recommend_arr, 'news');
                }

                //封面
                if (isset($_FILES) && $_FILES['news_images']['tmp_name'] != '')
                {
                    //生成时间路径
                    $time_path = File::create_time_path();
                    $file_detail = $this->save_simple_detail(NEWS_IMAGES_SAVE_PATH . '/' . $time_path, 'news_images');
                    if (Arr::validate_arr($file_detail))
                    {
                        foreach ($file_detail as $value)
                        {
                            $att_arr = array();
                            $att_arr['module'] = 'news';
                            $att_arr['file_name'] = $value['file_name'];
                            $att_arr['file_size'] = $value['file_size'];
                            $att_arr['file_ext'] = $value['file_ext'];
                            $att_arr['file_path'] = File::change_path($time_path);
                            $att_arr['file_realname'] = $value['file_realname'];
                            $att_arr['add_time'] = Date::get_time();
                            $att_arr['is_image'] = $value['is_image'];
                            //添加附件
                            $attachment_id = $this->news->add_attachment($att_arr);
                            Model::factory('news_attachment')->insert(array('news_id' => $id, 'attachment_id' => $attachment_id));
                        }
                    }
                }
                Message::success(__('add success'));
                $this->request->redirect('admin/news/add');
            }
        }
    }

}